extends Camera2D




func _ready() -> void:
	Game.game_camera = self
	reset_smoothing()


func _process(_delta: float) -> void:
	if Game.players.size():
		# 计算玩家的包围盒
		var total_pos := Vector2.ZERO
		for player in Game.players:
			total_pos += player.global_position
		var avg_pos := total_pos / Game.players.size()
		var camera_pos := Vector2(avg_pos.x, avg_pos.y)
		global_position = camera_pos

## 相机抖动
# length 总抖动次数
# shake_range 抖动像素范围
# freq 抖动间隔
func shake(length: int = 10, shake_range: int = 10, freq: float = 0.02) -> void:
	position_smoothing_enabled = false
	var old_pos := global_position
	for time in length:
		time += 1
		
		var offset := Vector2()
		offset.x = randi_range(-shake_range, shake_range)
		offset.y = randi_range(-shake_range, shake_range)
		
		var new_pos := old_pos
		new_pos += offset
		
		global_position = new_pos
		await get_tree().create_timer(freq).timeout
	
	global_position = old_pos
	position_smoothing_enabled = true
	

